package com.xbongbong.crm.handle.explain.impl;

import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.interfaces.explain.BusinessExplainHandle;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.pro.product.pojo.dto.FilterAttrOnOffJXCDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.OperatePageEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.RefundEnum;
import com.xbongbong.saas.help.DefaultAttrHelp;
import com.xbongbong.saas.help.ExplainHelp;
import com.xbongbong.saas.model.RefundModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author 吴峰
 * @date 2022/06/13 17:10
 */
@Service("refundExplainHandleImpl")
@Slf4j
public class RefundExplainHandleImpl implements BusinessExplainHandle {
    @Resource
    private RefundModel refundModel;
    @Resource
    private ExplainHelp explainHelp;
    @Resource
    private DefaultAttrHelp defaultAttrHelp;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.REFUND.getCode());
    }

    @Override
    public List<FieldAttrEntity> newHandleExplain(FormatExplainDTO formatExplainDTO) throws XbbException {
        List<FieldAttrEntity> explainList = formatExplainDTO.getExplainList();
        String corpid = formatExplainDTO.getCorpid();
        UserVO userVO = formatExplainDTO.getLoginUser();
        Set<String> permSet = userVO.getPermSet();
        FilterAttrOnOffJXCDTO filterAttrOnOffJXCDTO = new FilterAttrOnOffJXCDTO();
        BeanUtil.copyProperties(formatExplainDTO, filterAttrOnOffJXCDTO);
        filterAttrOnOffJXCDTO.setFieldAttrEntityList(explainList);
        filterAttrOnOffJXCDTO.setOperatePageCode(OperatePageEnum.NEW.getCode());
        refundModel.filterAttrOnOffJXC(filterAttrOnOffJXCDTO);
        for (FieldAttrEntity fieldAttrEntity : explainList) {
            Integer fieldType = fieldAttrEntity.getFieldType();
            String attr = fieldAttrEntity.getAttr();
            if (Objects.equals(fieldType, FieldTypeEnum.SELECT_PRODUCT.getType())) {
                explainHelp.formatSelectProductExplains(fieldAttrEntity, XbbRefTypeEnum.REFUND, corpid, OperatePageEnum.NEW.getCode(), false);
            }
            if (Objects.equals(attr, RefundEnum.SIGN_USER.getAttr())) {
                if(fieldAttrEntity.getDefaultAttr() == null || (fieldAttrEntity.getDefaultAttr() != null && fieldAttrEntity.getDefaultAttr().getDefaultList() == null)) {
                    fieldAttrEntity.setDefaultAttr(defaultAttrHelp.setUserDefaultAttrPoJo(formatExplainDTO.getUserId(), formatExplainDTO.getLoginUser().getName(),formatExplainDTO.getLoginUser().getAvatar()));
                }
            } else if(Objects.equals(attr, RefundEnum.PAYMENT_SHEETS.getAttr())) {
                //回款单新增权限判断
                if(!permSet.contains(ProPermissionAliasEnum.PAYMENT_SHEET_ADD.getAlias())) {
                    fieldAttrEntity.setEditable(BasicConstant.ZERO);
                }
            } else if(Objects.equals(attr, RefundEnum.PAYMENTS.getAttr())) {
                //红冲应收款权限判断
                if(!permSet.contains(ProPermissionAliasEnum.RED_RECEIVABLES.getAlias())) {
                    fieldAttrEntity.setEditable(BasicConstant.ZERO);
                }
            } else if(Objects.equals(attr, RefundEnum.INVOICES.getAttr())) {
                //红冲/作废发票按钮权限判断
                if(!permSet.contains(ProPermissionAliasEnum.INVOICE_CANCEL.getAlias()) && !permSet.contains(ProPermissionAliasEnum.INVOICE_RED.getAlias())) {
                    fieldAttrEntity.setEditable(BasicConstant.ZERO);
                }
            }
        }
        return explainList;
    }
}
